<template>
	<view class="hui">
		<view class="flex">
			<scroll-view class="left" :scroll-y="true" :show-scrollbar="false">
				<view @click="setCateCheck(item)" :class="['cate',item.id==cateCheck.id?'cate_check':'']" v-for="(item,index) in cate" :key="index">
					{{item.name}}
				</view>
			</scroll-view>

			<view class="right_box">
				<view class="rhead">
					<scroll-view  :scroll-left="scrollLeft" :scroll-with-animation="true" class="rsroll" :scroll-x="true" :show-scrollbar="false">
						<view @click="setChildCate(item,index)" :class="['childcate',item.id==childcateCheck.id?'childcate_check':'']"
							v-for="(item,index) in childcate" :key="index">
							{{item.name}}
						</view>
					</scroll-view>
					<view class="rxia" @click="show=true">
						<view class="rixa_down">
							<u-icon name="arrow-down" color="#6C6C6C" size="28"></u-icon>
						</view>
					</view>
				</view>
				<view style="height: 50px;"></view>

				<scroll-view :scroll-top="scrollTop" :scroll-with-animation="true" :scroll-y="true" :show-scrollbar="false" class="list_box">
					<view class="list">
						
					<view  v-for="(item,index) in childcate" :key="index">
						<view class="name_box">
							<view class="name">{{item.name}}</view>
						    <u-icon name="arrow-right" color="#6E6E6E" size="22"></u-icon>	
						</view>
						<view class="icon_box">
							<view @click="toSearch(item,items)" class="icon_info" v-for="(items,indexs) in item.children" :key="indexs">
								<image mode="widthFix" :src="items.image"></image>
								<view class="icon_name">{{items.name}}</view>
							</view>
						</view>
					</view>
					
					</view>
				</scroll-view>


			</view>

		</view>
		
		
		<u-popup v-model="show" mode="top" border-radius="14">
			<view class="top_box">
				<view class="t_head">
					<view>{{cateCheck.name}}</view>
					 <u-icon name="arrow-up" color="#6E6E6E" size="22"></u-icon>
				</view>
				<view class="icon_box">
					<view @click="setChildCate(item,index)" :class="['top_info',item.id==childcateCheck.id?'top_check':'']"  v-for="(item,index) in childcate" :key="index">
						{{item.name}}
					</view>
				</view>
			</view>
		</u-popup>
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cate_id:'',
				cateCheck:{id:0},
				cate: [],
				childcateCheck:{id:0},
				childcate: [],
				show:false,
				scrollLeft:0,
				scrollTop:0
			}
		},
		onLoad(e) {
			if(e.cate_id){
				this.cate_id=e.cate_id;
			}
			this.getCate();
		},
		methods: {
			toSearch(e,es){
				uni.navigateTo({
					url:'/pages/index/search?cate_id='+es.id+'&pid='+e.id
				})
			},
			setChildCate(item,e){
			  this.show=false;
			  let width=0;
			  let top=0;
			  this.childcateCheck=item;
			  this.childcate.forEach((res,index)=>{
				  
				  if(index>=e) return;
				   width+=res.name.length*15+10;
				   top+=Math.ceil(res.children.length/3)*71.33+57;
			  });
			  
			 
			  
			  console.log(width);
			  
			  this.scrollLeft=width; 
			  this.scrollTop=top;
			},
			setCateCheck(e){
				this.cateCheck=e;
				this.scrollLeft=0;
				this.scrollTop=0;
				this.getChildCate();
			},
			getChildCate() {
				let parm = {
					cate_id: this.cateCheck.id
				};
				this.$get(this.$api.goodChild_cate, parm, res => {
					let data = res.data;
					if (data.code == 1) {
						this.childcate = data.data;
						if (data.data && data.data.length > 0) {
							this.childcateCheck = data.data[0];
						}
					}
				});
			},
			getCate() {
				this.$get(this.$api.goodCate, {}, res => {
					let data = res.data;
					if (data.code == 1) {
						this.cate = data.data;
						if (data.data && data.data.length > 0) {
							
							
							if(this.cate_id){
								data.data.forEach(e=>{
									if(e.id==this.cate_id){
										this.cateCheck=e;
										return;
									}
								})
							}else{
								this.cateCheck = data.data[0];
							}
							
							this.getChildCate();
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	scroll-view ::-webkit-scrollbar {
		display: none;
		width: 0;
		height: 0;
		color: transparent;
	}

	.hui {
		background-color: #ffffff;
		min-height: 100vh;
	}

	.flex {
		display: flex;
		align-items: center;
	}

	.left {
		height: 100vh;
		width: 25%;
		text-align: center;
		background-color: #F4F4F4;
	}

	.right_box {
		height: 100vh;
		width: 75%;
		background-color: #F4F4F4;
		position: relative;
		border-radius: 25rpx;
	}

	.cate {
		width: 100%;
		height: 100rpx;
		line-height: 100rpx;
		color: #737373;
		font-size: 28rpx;
	}

	.cate_check {
		font-weight: bold;
		font-size: 32rpx;
		color: #f44b44;
	}

	.rhead {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		display: flex;
		align-items: center;
		height: 100rpx;
		background-color: #F4F4F4;
	}

	.rxia {
		width: 15%;
		height: 100%;
		background-color: #F4F4F4;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.rixa_down {
		width: 56rpx;
		height: 56rpx;
		line-height: 56rpx;
		border-radius: 50%;
		background-color: white;
		text-align: center;

	}

	.rsroll {
		width: 85%;
		display: flex;
		align-items: center;
		background-color: #F4F4F4;
		box-sizing: border-box;
		//padding: 0 0 20rpx 0;
		white-space: nowrap;
	}

	.childcate {
		display: inline-block;
		background-color: white;
		box-sizing: border-box;
		padding: 10rpx 25rpx;
		color: black;
		border-radius: 35rpx;
		font-size: 26rpx;
		margin-right: 20rpx;
	}

	.childcate_check {
		background-color: #FEE9E8;
		border: 1rpx solid #f44b44;
		color: #f44b44;
		padding: 9rpx 25rpx;
	}

	.name_box {
		padding-top: 25rpx;
		margin-left: 20rpx;
		display: flex;
		align-items: center;
	}

	.name {
		font-weight: bold;
		color: black;
	     margin-right:15rpx;
	}
	.list_box{
		
		height: calc(100vh - 50px);
	}
	.list{
		border-radius: 25rpx;
		background-color: white;
		min-height: 100%;
		box-sizing: border-box;
		padding-bottom: 25rpx;
	}
	.icon_box{
		display: flex;
		align-items: center;
		flex-wrap: wrap;
	}
	.icon_info{
		width: 33%;
		margin-top: 25rpx;
		text-align: center;
		image{
			width: 70%;
			height: 100rpx;
		}
	}
	.icon_name{
		color:#7D7D7D;
		font-size: 26rpx;
	}
	
	.top_box{
		box-sizing: border-box;
		padding:25rpx 0 25rpx 20rpx;
		background-color: #F6F6F6;
	}
	
	.t_head{
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-weight: bold;
		color: black;
			box-sizing: border-box;
			padding-right: 20rpx;
	}
	.top_info{
		width: 30%;
		text-align: center;
		height: 65rpx;
		line-height: 65rpx;
		margin-top: 25rpx;
		margin-right: 25rpx;
		background-color:#EAEAEA;
		color:#6F6F6F;
		border-radius: 35rpx;
		font-size: 26rpx;
	}
	.top_check{
		background-color: #FEE9E8;
		border: 1rpx solid #f44b44;
		color: #f44b44;
		height: 64rpx;
		line-height: 64rpx;
	}
	
</style>